package moltencore.network.serverlobby.strategy;

import spark.network.protocol.Base;
import moltencore.network.protocol.Logout;
import spark.network.protocol.Response;
import spark.network.strategy.IProtocolHandleStrategy;
import moltencore.player.PlayerList;
import moltencore.player.Player;
import moltencore.player.exception.PlayerNotLoggedInException;
import org.apache.log4j.Logger;
import spark.utility.OutWrapper;

import java.util.Collection;

public class LogoutStrategy  implements IProtocolHandleStrategy<Logout> {
	private Logger logger = Logger.getLogger(LogoutStrategy.class);
	private PlayerList<Player> playerList;

	public LogoutStrategy(PlayerList<Player> playerList) {
		this.playerList = playerList;
	}

	@Override
	public void handle(Logout packet, OutWrapper<Response> acknowledgement, Collection<Base> additionalData) {
		try {
			playerList.logout(packet.getSession());
		}
		catch(PlayerNotLoggedInException e) {
			logger.info("Player with session " + packet.getSession() + " could not be logged out: Not logged in", e);
		}
	}
}
